package agents;

import java.util.logging.Level;
import java.util.logging.Logger;
import negotiator.Bid;
import negotiator.issue.ISSUETYPE;
import negotiator.issue.Issue;
import negotiator.issue.Value;
import negotiator.issue.ValueDiscrete;
import negotiator.issue.ValueInteger;
import negotiator.issue.ValueReal;

/**
 *
 * @author Oliver Trinnes, Thomas Nowicki, Felix Remmel, Ruben Schwarzwald
 */
public class IssueValue
{
  private Issue issue;
  private Value value;

  public IssueValue(Issue issue, Bid bid)
  {
    this.issue = issue;
    try
    {
      this.value = bid.getValue(issue.getNumber());
    }
    catch(Exception ex)
    {
      Logger.getLogger(IssueValue.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public Value getValue()
  {
    return value;
  }

  public Issue getIssue()
  {
    return issue;
  }

  @Override
  public boolean equals(Object obj)
  {
    if(!(obj instanceof IssueValue))
      return false;

    IssueValue compareIssueValue = (IssueValue)obj;

    ISSUETYPE type = compareIssueValue.getIssue().getType();
    if(!type.equals(issue.getType()))
      return false;
    if(issue.getNumber() != compareIssueValue.getIssue().getNumber())
      return false;

    switch(issue.getType())
    {
      case DISCRETE:
        ValueDiscrete valueDiscrete = (ValueDiscrete)value;
        ValueDiscrete valueDiscreteCompare = (ValueDiscrete)compareIssueValue.
            getValue();
        String string = valueDiscrete.getValue();
        String stringCompare = valueDiscreteCompare.getValue();
        return string.equals(stringCompare);
      case REAL:
        ValueReal valueReal = (ValueReal)value;
        ValueReal valueRealCompare = (ValueReal)compareIssueValue.
            getValue();
        double d = valueReal.getValue();
        double dCompare = valueRealCompare.getValue();
        return d == dCompare;
      case INTEGER:
        ValueInteger valueInteger = (ValueInteger)value;
        ValueInteger valueIntegerCompare = (ValueInteger)compareIssueValue.
            getValue();
        int i = valueInteger.getValue();
        int iCompare = valueIntegerCompare.getValue();
        return i == iCompare;
    }
    return false;
  }

  @Override
  public int hashCode()
  {
    int hash = 5;
    hash = 83 * hash + (this.value != null ? this.value.hashCode() : 0);
    return hash;
  }

}
